<?php

function getPhoneNumber($code){
	$appid = 'wxdaca5fbf3f5d8928';
	$AppSecret = 'd97604c61c3674843b97ee213791a570';
	//1 获取 ACCESS_TOKEN https://api.weixin.qq.com/cgi-bin/token
	$url                    = 'https://api.weixin.qq.com/cgi-bin/token?appid='.$appid.'&secret='.$AppSecret.'&js_code='.$code.'&grant_type=client_credential';
	$curl_return_data       = curlGet($url);
	$curl_return_ary       =@json_decode($curl_return_data, true);
	$access_token           = $curl_return_ary['access_token'];

	$url                    = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token;
	$curl_return_data      = postCurl($url, ['code'=>$code], true);
	$curl_return_ary       =@json_decode($curl_return_data, true);
	if(!$curl_return_ary || empty($curl_return_ary['phone_info']))
	{
	}
	$phoneNumber        = $curl_return_ary['phone_info']['phoneNumber'];
	return $curl_return_ary;
}
function curlGet($url, &$httpCode = 0)
{
	//1、初始化curl
	$curl = curl_init();

	//2、告诉curl,请求的地址
	curl_setopt($curl, CURLOPT_URL, $url);
	//3、将请求的数据返回，而不是直接输出
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);

	$fileContents = curl_exec($curl); // 执行操作
	curl_close($curl); // 关键CURL会话

	return $fileContents; // 返回数据
}
function postCurl($url,$data,$json =false){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($data)) {
        if($json && is_array($data)){
            $data = json_encode( $data );
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        if($json){ //发送JSON数据
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_HTTPHEADER,
                array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data)));}
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($curl);
    $errorno = curl_errno($curl);
    if ($errorno) {
        return array('errorno' => false, 'errmsg' => $errorno);
    }
    curl_close($curl);
    return $res;
}

exit(json_encode(getPhoneNumber($_REQUEST['code'])));